package net.sk.mina.imagine;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class ImgServlet extends HttpServlet {
	static final String SPLIT_SNO = "-";
	static HashMap<Integer,BufferedImage> mapImg = new HashMap<Integer,BufferedImage>();
	
	public static void setImg(Integer key,BufferedImage val){
		mapImg.put(key, val);
	}
  public void doGet( HttpServletRequest request , HttpServletResponse response )  throws ServletException , IOException{
	  String sno = request.getParameter("sno");
	  int pos = sno.indexOf(SPLIT_SNO);
	  Integer sid = Integer.parseInt(sno.substring(0, pos));
	  BufferedImage img= mapImg.get(sid);
	  if(img==null){
		  response.sendError(404);
	  }
	ServletOutputStream sos = response.getOutputStream(); //取得输出流，这里是把图片输出到客户端，如果想把图片生成本地图片文件可以这样

	//FileOutputStream sos=new FileOutputStream(new File("C:/cs.jpg"));
	BufferedOutputStream bos = new BufferedOutputStream(sos); //生成输出缓存
	//JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); //生成JPEG图片对象
	//encoder.encode(img); //按格式把图片流进行编码
	bos.close();
	sos.close();
	  
  }

}
